<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Abstract
 *
 * @author Kopi
 */
abstract class ModelAbstract
{
    //put your code here
    
    abstract public function getId();
    abstract public function setId($id);
    abstract public function getFields();
    abstract protected function getClassName();

    protected $xml;

    /**
     * generate private variables code
     */
    public function showPrivateVariablesCode(){
        foreach ($this->fields AS $field){
            echo '      private $'.$field.';
';
        }
    }

    /**
     * import data from sql array
     * @param array $data 
     */
    public function __fromArray($data){        
        
        foreach ($this->fields AS $key=>$field){            
            if (isset($data[$key])){
                $method = 'set'.$field;
                $this->$method($data[$key]);
            }
        }
    }
    /**
     * import data from sql array
     * @param array $data 
     */
    public function __fromFormArray($data){        
        
        foreach ($this->fields AS $key=>$field){            
            if (isset($data[$field])){
                $method = 'set'.$field;
                $this->$method($data[$field]);
            }
        }
                
    }
    
    
    public function upFirstLetters($text){
        return ucfirst($text);
        $text = $this->Lower($text);
        $textArray = explode(' ', $text);
        $textReturn = '';
        foreach ($textArray AS $text){
            if (strlen($text)>3)
                $textReturn .= ' '.ucfirst($text);
            else
                $textReturn .= ' '.($text);
        }
        return substr($textReturn,1);
    }
    public function getIconExtension($name=null){
        if ($name == null)
            return substr($this->getIcon(), strrpos($this->getIcon(), '.'));
        else
            return substr($name, strrpos($name, '.'));
    }
    
    public function getSqlColumn($varName){
        foreach ($this->fields AS $key=>$value){
            if ($value == $varName)
                return $key;
        }
        return null;
    } 
    
    /**
     * Import danych z tablicy asocjacyjnej do p�l klasy.
     *
     * @param string $array tablica asocjacyjna zawieraj�ca rekordy pobrane z bazy
     * @param int $cut 1 - klucze musz� mie� posta� <KLASA>_<POLE>, 0 - <POLE>
     */
    public function FromArray($array, $cut = false, $alias = null) {
	$fields = $this->GetFields();
	// sprawdzenie czy istnieje metoda dla pol niebedacych w BD
	if (method_exists($this, 'GetClassFields')) {
	    $fields = array_merge($fields, $this->GetClassFields());
	}
	
	$prfx = ((!empty($alias) && is_string($alias)) ? $alias . '_' : '' );
	if (is_array($array))
	    foreach ($array as $index => $var) {
		if ($cut == true)
		    $index = str_replace($prfx . $this->getClassName() . "_", "", $index);
		if (array_key_exists($index, $fields)) {
		    $temp = 'Set' . $fields[$index];
		    $this->$temp($var);
		}
	    }
    }    
    
    
 
    
}
?>
